package com.penghaisoft.microwms.producthand.web.controller.insidewarehouse;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.controller.BaseController;
import com.penghaisoft.microwms.common.dto.HandResp;
import com.penghaisoft.microwms.common.dto.ProCountingDto;
import com.penghaisoft.microwms.common.dto.RawCountingDto;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.common.page.Pager;
import com.penghaisoft.microwms.common.wms.model.ProCountingDifference;
import com.penghaisoft.microwms.common.wms.model.ProCountingRecords;
import com.penghaisoft.microwms.common.wms.model.ProCountingTask;
import com.penghaisoft.microwms.common.wms.model.ProStorageSn;
import com.penghaisoft.microwms.common.wms.model.RawCountingDifference;
import com.penghaisoft.microwms.common.wms.model.RawCountingRecords;
import com.penghaisoft.microwms.common.wms.model.RawCountingTask;
import com.penghaisoft.microwms.common.wms.model.RawStorageSn;
import com.penghaisoft.microwms.product.service.IProCountingDifferenceService;
import com.penghaisoft.microwms.product.service.IProCountingRecordsService;
import com.penghaisoft.microwms.product.service.IProCountingTaskService;
import com.penghaisoft.microwms.product.service.IProOrderInSnService;
import com.penghaisoft.microwms.product.service.IProStorageSnService;

/**
 * @ClassName: ProCountingHandController
 * @Description: 盘点手持
 * @author jzh
 * @date 2018年6月13日 上午9:41:25
 */
@RestController
@RequestMapping(value = "/pro/countinghand")
public class ProCountingHandController extends BaseController {

	@Autowired
	private IProCountingTaskService proCountingTaskService;
	
	@Autowired
	private IProStorageSnService proStorageSnService;

	@Autowired
	private IProCountingRecordsService proCountingRecordsService;
	
	@Autowired
	private IProCountingDifferenceService proCountingDifferenceService;
	
	
	/**
	 * 查询列表
	 * 
	 * @param pager
	 * @return
	 */
	@PostMapping("list")
	public TransResult list(@RequestParam(name = "page", defaultValue = "1") int page,
			@RequestParam(name = "rows", defaultValue = "10") int rows, ProCountingTask proCountingTask) {
		Pager<ProCountingTask> result = proCountingTaskService.findListByCondition(page, rows, proCountingTask);
		return success(result);
	}
	
	
    /** 
    * @Title: countingCodeExist 
    * @Description: 判断盘点单号是否存在
    * @param @param rawBackDto
    * @param @return  参数说明 
    * @return HandResp    返回类型
    * @author jzh 
    * @throws 
    */
    @PostMapping("countingcode")
    public HandResp countingCodeExist(@RequestBody ProCountingDto proCountingDto){
        HandResp result = new HandResp();
        //Code先置为1SUCCESS
        result.setCode(Constant.SUCCESS);
        if (proCountingDto.getCountingCode().equals("")||proCountingDto.getCountingCode()==null){
            result.setCode(Constant.FAIL);
            result.setMsg("请输入盘点单号！");
            return result;
        }
        //设置查询标志（CountingCode=？ ，ActiveFlag = 1）
        ProCountingTask proCountingTask = new ProCountingTask();
        proCountingTask.setCountingCode(proCountingDto.getCountingCode());
        proCountingTask.setActiveFlag("1");
       
        List<ProCountingTask> tmp = proCountingTaskService.queryByAny(proCountingTask);
        if(tmp.size() > 0){
        	if(tmp.get(0).getStauts().equals("0")){
        		result.setMsg("盘点任务存在，请盘点");
        		//记录last_modified_by，gmt_modified	最后修改人，最后修改时间
        		proCountingTask.setLastModifiedBy(proCountingDto.getLoginName());
        		proCountingTask.setGmtModified(new Date());
        		//根据countingTaskId修改
        		proCountingTask.setCountingTaskId(tmp.get(0).getCountingTaskId());
        		Resp resp = new Resp();
        		//将盘点任务状态置为盘点中
        		resp = proCountingTaskService.updateProCountingTask(proCountingTask);
        		result.setCode(resp.getCode());
        		if (resp.getMsg() != null && !resp.getMsg().equals("")) {
        			result.setMsg(resp.getMsg());
        		}
        	}else if(tmp.get(0).getStauts().equals("1")){
//        		result.setCode(Constant.FAIL);
            	result.setMsg("继续盘点");
        	}else if(tmp.get(0).getStauts().equals("2")){
        		result.setCode(Constant.FAIL);
            	result.setMsg("盘点任务已完成");
        	}else{
        		result.setCode(Constant.FAIL);
        		result.setMsg("盘点任务未知状态");
        	}
        }else{
        	result.setCode(Constant.FAIL);
        	result.setMsg("盘点任务不存在！");
        }
        return result;
    }
    
    /** 
    * @Title: barcodeExist 
    * @Description: 扫码盘点
    * @author jzh 
    */
    @PostMapping("barcodeisexist")
    public HandResp barcodeExist(@RequestBody ProCountingDto proCountingDto){
        HandResp result = new HandResp();
        //入参条码，盘点单号，LoginName
        String barcode= proCountingDto.getBarcode();
        String countingCode = proCountingDto.getCountingCode();
        String LoginName = proCountingDto.getLoginName();
        //根据盘点单号查询合同号,型号类型
        ProCountingTask countingByCode = new ProCountingTask();
        countingByCode.setCountingCode(countingCode);
        List<ProCountingTask> tmp = proCountingTaskService.queryByAny(countingByCode);
        String contractNo = tmp.get(0).getContractNo();
        //到rawStorageSn中查询(两种盘点类型)
        ProStorageSn proStorageSn = new ProStorageSn();
        if(tmp.get(0).getModelType()!=null||!tmp.get(0).getModelType().equals("")){
        	//类型盘点
        	String modelType = tmp.get(0).getModelType();
        	//根据条码，合同号,类型 到rawStorageSn中查询，判断当前条码是否在合同号下
        	proStorageSn.setModelType(modelType);
            proStorageSn.setBarcode(barcode);
            proStorageSn.setContractNo(contractNo);
            proStorageSn.setActiveFlag("1");
        }else{
        	//合同号盘点
	        //根据条码，合同号到rawStorageSn中查询，判断当前条码是否在合同号
	        proStorageSn.setBarcode(barcode);
	        proStorageSn.setContractNo(contractNo);
	        proStorageSn.setActiveFlag("1");
        }
        List<ProStorageSn> list = proStorageSnService.queryByAny(proStorageSn);
        if(list.size()>0){
        	//当前条码存在合同号下
        	ProCountingRecords proCountingRecords = new ProCountingRecords();
        	proCountingRecords.setCountingCode(countingCode);
        	proCountingRecords.setBarcode(barcode);
        	//查看这个条码是否已经被盘点了countingCode barcode
        	List<ProCountingRecords> proCountingRecordsList =proCountingRecordsService.queryByAny(proCountingRecords);
        	if(proCountingRecordsList.size()>0){
        		result.setCode(Constant.FAIL);
            	result.setMsg("改条码已经被盘点了，请继续扫描");
        	}else{
        		//创建盘点记录
        		proCountingRecords.setCountingTime(new Date());
        		proCountingRecords.setModel(list.get(0).getModel());
        		proCountingRecords.setModelDesc(list.get(0).getModelDesc());
        		proCountingRecords.setModelType(list.get(0).getModelType());
        		proCountingRecords.setWhCode(list.get(0).getWlCode());
        		proCountingRecords.setWdCode(list.get(0).getWdCode());
        		proCountingRecords.setWaCode(list.get(0).getWaCode());
        		proCountingRecords.setWlCode(list.get(0).getWlCode());
        		proCountingRecords.setKeepBy(LoginName);
        		proCountingRecords.setCreateBy(LoginName);
        		proCountingRecords.setGmtCreate(new Date());
        		proCountingRecords.setActiveFlag("1");
        		if(list.get(0).getModelType()!=null||!list.get(0).getModelType().equals("")){
        			proCountingRecords.setModelType(list.get(0).getModelType());
        		}
		    	Resp resp = proCountingRecordsService.create(proCountingRecords);
		    	//返回手持页面信息（当前扫描条码信息）
		        List Countinglist = new ArrayList<>();
		        if(list.get(0).getModelType()!=null||!list.get(0).getModelType().equals("")){
		        	proCountingDto.setModelType(list.get(0).getModelType());
        		}
		        proCountingDto.setModel(list.get(0).getModel());
		        proCountingDto.setModelDesc(list.get(0).getModelDesc());
		        proCountingDto.setModelType(list.get(0).getModelType());
		        proCountingDto.setWhCode(list.get(0).getWlCode());
		        proCountingDto.setWdCode(list.get(0).getWdCode());
		        proCountingDto.setWaCode(list.get(0).getWaCode());
		        proCountingDto.setWlCode(list.get(0).getWlCode());
		        proCountingDto.setAmount(list.get(0).getAmount());
		        proCountingDto.setKeepBy(LoginName);
		        //当前盘点数量
		        int nowCountingNum = 0;
		        //根据盘点单号在盘点记录表中查询已盘点数量
		        nowCountingNum = proCountingRecordsService.querCountByCountingCode(countingCode);
		        proCountingDto.setNowCountingNum(nowCountingNum);
		        Countinglist.add(proCountingDto);
	            result.setList(Countinglist);
	            result.setCode(resp.getCode());
	            result.setMsg(resp.getMsg());
        	}
        
        }else{
        	result.setCode(Constant.FAIL);
        	result.setMsg("当前条码不是该合同或类型的条码，请继续扫描");
        }
        return result;
    }
    
    
    /** 
    * @Title: countingConfig 
    * @Description: 成品盘点确认
    * @param @param rawCountingDto
    * @param @return  参数说明 
    * @return HandResp    返回类型
    * @author jzh 
    * @throws 
    */
    @PostMapping("countingconfig")
    public HandResp countingConfig(@RequestBody ProCountingDto proCountingDto){
        HandResp result = new HandResp();
        //入参：盘点单号，LoginName
        String countingCode = proCountingDto.getCountingCode();
        String LoginName = proCountingDto.getLoginName();
        //根据盘点单号查询合同号
        ProCountingTask countingByCode = new ProCountingTask();
        countingByCode.setCountingCode(countingCode);
        List<ProCountingTask> tmp = proCountingTaskService.queryByAny(countingByCode);
        if(tmp.size()>0){
	        String contractNo = tmp.get(0).getContractNo();
	        proCountingDto.setContractNo(contractNo);
	        if(tmp.get(0).getModelType()!=null||!tmp.get(0).getModelType().equals("")){
	        	proCountingDto.setModelType(tmp.get(0).getModelType());
	        }
	        //根据盘点单号，合同号 (型号类型)查询盘亏数据
	        List<ProStorageSn> listLose = proStorageSnService.queryLose(proCountingDto);
	        if(listLose.size()>0){
	        	//盘亏，存在盘亏数据
	        	for(ProStorageSn proStorageSn:listLose){
	        		//创建盘亏差异记录
	        		ProCountingDifference proCountingDifference = new ProCountingDifference();
	        		proCountingDifference.setCountingCode(countingCode);
	        		proCountingDifference.setContractNo(contractNo);
	        		proCountingDifference.setPurchaseNo(proStorageSn.getPurchaseNo());
	        		proCountingDifference.setCountingTime(new Date());//盘点时间记录当前时间了
	        		proCountingDifference.setProBarcode(proStorageSn.getBarcode());
	        		proCountingDifference.setModel(proStorageSn.getModel());
	        		proCountingDifference.setModelDesc(proStorageSn.getModelDesc());
	        		proCountingDifference.setModelType(proStorageSn.getModelType());
	        		proCountingDifference.setDifferType("1");//盘亏
	        		proCountingDifference.setDifferStatus("0");//未处理
	        		proCountingDifference.setWaCode(proStorageSn.getWaCode());
	        		proCountingDifference.setWdCode(proStorageSn.getWdCode());
	        		proCountingDifference.setWhCode(proStorageSn.getWhCode());
	        		proCountingDifference.setWlCode(proStorageSn.getWlCode());
	        		proCountingDifference.setKeepBy(LoginName);//保管员记录为当前登录人了
	        		proCountingDifference.setGmtCreate(new Date());
	        		proCountingDifference.setActiveFlag("1");
	        		proCountingDifference.setCreateBy(LoginName);
	        		Resp resp = proCountingDifferenceService.create(proCountingDifference);	
	        	}
	        }
	        //根据盘点单号，合同号 查询盘盈数据
	        List<ProCountingRecords> listWin = proCountingRecordsService.queryWin(proCountingDto);
	        if(listWin.size()>0){
	        	for(ProCountingRecords proCountingRecords : listWin){
	        		//创建盘盈差异记录
	        		ProCountingDifference proCountingDifference = new ProCountingDifference();
	        		proCountingDifference.setCountingCode(countingCode);
	        		proCountingDifference.setContractNo(contractNo);
	        		//proCountingDifference.setPurchaseNo(rawCountingRecords.getPurchaseNo());
	        		proCountingDifference.setCountingTime(new Date());//盘点时间记录当前时间了
	        		proCountingDifference.setProBarcode(proCountingRecords.getBarcode());
	        		proCountingDifference.setModel(proCountingRecords.getModel());
	        		proCountingDifference.setModelDesc(proCountingRecords.getModelDesc());
	        		proCountingDifference.setModelType(proCountingRecords.getModelType());
	        		proCountingDifference.setDifferType("0");//盘盈
	        		proCountingDifference.setDifferStatus("0");//未处理
	        		proCountingDifference.setWaCode(proCountingRecords.getWaCode());
	        		proCountingDifference.setWdCode(proCountingRecords.getWdCode());
	        		proCountingDifference.setWhCode(proCountingRecords.getWhCode());
	        		proCountingDifference.setWlCode(proCountingRecords.getWlCode());
	        		proCountingDifference.setKeepBy(LoginName);//保管员记录为当前登录人了
	        		proCountingDifference.setGmtCreate(new Date());
	        		proCountingDifference.setActiveFlag("1");
	        		proCountingDifference.setCreateBy(LoginName);
	        		Resp resp = proCountingDifferenceService.create(proCountingDifference);	
	        	}
	        }
	        ProCountingTask proCountingTask = new ProCountingTask();
	        proCountingTask.setLastModifiedBy(proCountingDto.getLoginName());
	        proCountingTask.setGmtModified(new Date());
			//根据countingTaskId修改
	        proCountingTask.setCountingTaskId(tmp.get(0).getCountingTaskId());
			Resp resp = new Resp();
			//将盘点任务状态置为盘点结束
			proCountingTask.setStauts("2");
			resp = proCountingTaskService.update(proCountingTask);
			
	        if(listWin.size()==0&&listLose.size()==0){
	        	result.setCode(Constant.SUCCESS);
	        	result.setMsg("盘点无差异");
	        }else{
	        	result.setCode(Constant.SUCCESS);
	        	result.setMsg("盘点差异已记录");
	        }
        }else{
        	result.setCode(Constant.FAIL);
        	result.setMsg("盘点单号不存在");
        }
         
        return result;
    }

}
